import React from 'react'
import qqqq from 'prop-types'
export class Provider extends React.Component{
  static childContextTypes = {
    store:qqqq.object
  }
  getChildContext(){
    return {
      store:this.props.store
    }
  }
  render(){
    return <>
      {this.props.children}
    </>
  }
}
export  const connect = function(mapStateToProps,mapDispatchToProps){
  return function (Com){
    return class Temp extends React.Component{
      static contextTypes = {
        store:qqqq.object
      }
      constructor(props,context){
        super(props,context);
        // 吧redux的state转成这个临时组建的 私有状态
        this.state = mapStateToProps(context.store.getState())
      }
      componentDidMount(){
        this.qqq = this.context.store.subscribe(()=>{
          this.setState(mapStateToProps(this.context.store.getState()))
        })
      }
      componentWillUnmount(){
        this.qqq();
      }
      render(){
        return <Com  {...this.state} {...mapDispatchToProps(this.context.store.dispatch)}></Com>
      }
    }
  }
}